package ph.gov.marikina.mcsmtms.tests;

import java.util.List;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.client.RestTemplate;

import ph.gov.marikina.mcsmtms.domain.SituationReport;
import ph.gov.marikina.mcsmtms.domain.User;
import ph.gov.marikina.mcsmtms.rest.webservices.client.BasicSecureSimpleClientHttpRequestFactory;
import ph.gov.marikina.mcsmtms.rest.webservices.client.SimpleCredentials;
import ph.gov.marikina.mcsmtms.rest.webservices.client.SimpleCrendentialsProvider;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;


public class UserControllerTest {

	Logger log = Logger.getLogger(UserControllerTest.class);

	/** The context. */
	// private static ApplicationContext springContext = new
	// ClassPathXmlApplicationContext(
	// "spring/test-spring-config.xml");

//	private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
//			"test-applicationContext.xml");

	private static final String USER_REST_SERVICE_URL = "http://localhost:8888/rest/users/";

	private RestTemplate restTemplate;

	/** The application context. */
	// private static ApplicationContext applicationContext = new
	// ClassPathXmlApplicationContext(
	// "applicationContext.xml");

	@Before
	public void setUp() throws Exception {
		restTemplate = new RestTemplate();
	}

	@Test
	public void getUserTest() {
		
		String username = "rix";
		String password = "1";
		BasicSecureSimpleClientHttpRequestFactory factory = new BasicSecureSimpleClientHttpRequestFactory();
		factory.setCredentialsProvider(new SimpleCrendentialsProvider(new SimpleCredentials(username + ":" + password, "1")));
		restTemplate.setRequestFactory(factory);
		String result = restTemplate.getForObject(USER_REST_SERVICE_URL + username, String.class);
        
        Gson gson = new Gson();
        JsonElement jElement = new JsonParser().parse(result);
        JsonObject jObject = jElement.getAsJsonObject();
        User user = gson.fromJson(jObject.get("user"), new TypeToken<User>(){}.getType());
        
        System.out.println(result);
        
	    assert(user != null);
	    System.out.println("username: " + user.getUserName());
	    System.out.println("password: " + user.getPassword());
	}
}

